﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace FileMover.Service.Actions
{
    public class MoveToActionProvider : ActionFactoryBase<MoveToAction>
    {
        public MoveToActionProvider()
        {
        }

        public override string ToString()
        {
            return "Move To"; ///TODO: Localize
        }

        protected override string XmlLocalName
        {
            get { return "MoveTo"; }
        }
        protected override string XmlNameSpace
        {
            get { return Model.mgr.LookupNamespace("FileMover"); }
        }

    }
    public class MoveToAction : RuleItemBase, IAction
    {
        private string _target = null;

        [Browsable(true)]
        public string Target
        {
            get { return _target; }
            set
            {
                if (string.Equals(value, _target))
                    return;
                _target = value;
                if (_elem != null)
                {
                    _elem.SetAttribute("target", _target);
                }
            }
        }
        public override void FromXml(System.Xml.XmlElement elem)
        {
            base.FromXml(elem);
            _target = elem.GetAttribute("target");
        }

        public override string DisplayString()
        {
            return string.Format("Action: Move to {0}", Target);
        }

        #region IAction Member

        public void Process(ILog log, bool TestMode, List<IItem> items)
        {
            foreach (var item in items)
            {
                var target = ItemHelper.ResolveVars(item, Target, null);
                log.Log(LevelEnum.Info, "Move: {0} => {1}\\{2}{3}", item.GetProperty("fullname"), target, item.GetProperty("filename"), item.GetProperty("extension"));
                item.Handled = true;
            }
        }

        #endregion
    }
}
